package com.yzcrm.erp.crm.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yzcrm.common.annotation.NotNullValidate;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.admin.service.AdminSceneService;
import com.yzcrm.erp.crm.entity.CrmBusiness;
import com.yzcrm.erp.crm.entity.CrmPreSales;
import com.yzcrm.erp.crm.service.CrmPreSalesService;
import com.yzcrm.utils.R;

public class CrmPreSalesController extends Controller {
	@Inject
	private CrmPreSalesService crmPreSalesService;
	@Inject
    private AdminSceneService adminSceneService;
	/**
     * @author lg
     * 新增或更新售前申请
     */
    @Permissions({"crm:preSales:save","crm:preSales:update"})
    public void addOrUpdate(){
        JSONObject object= JSON.parseObject(getRawData());
        renderJson(crmPreSalesService.addOrUpdate(object));
    }
    
    /**
     * @author lg
     * 根据售前申请id查询
     */
    @Permissions("crm:preSales:read")
    @NotNullValidate(value = "preSalesId",message = "售前申请id不能为空")
    public void queryById(@Para("preSalesId")Integer preSalesId){
        renderJson(R.ok().put("data",crmPreSalesService.queryById(preSalesId)));
    }
    
    /**
     * @author lg
     * 根据售前id变更负责人
     */
    @Permissions("crm:preSales:changeOwnerUser")
    @NotNullValidate(value = "preSalesIds",message = "售前id不能为空")
    @NotNullValidate(value = "newOwnerUserId",message = "负责人id不能为空")
    @NotNullValidate(value = "transferType",message = "移除方式不能为空")
    @Before(Tx.class)
    public void changeOwnerUser(@Para("")CrmPreSales crmPreSales){
        renderJson(crmPreSalesService.updateOwnerUser(crmPreSales));
    }
    
    /**
     * @author lg
     * 接受售前申请
     */
    @Permissions("crm:preSales:accept")
    @NotNullValidate(value = "preSalesIds",message = "售前id不能为空")
    @Before(Tx.class)
    public void acceptPreSales(@Para("")CrmPreSales crmPreSales){
        renderJson(crmPreSalesService.acceptPreSales(crmPreSales));
    }
    
    /**
     * @author lg
     * 查看列表页
     */
    @Permissions({"crm:preSales:index"})
    public void queryPageList(BasePageRequest basePageRequest) {
        JSONObject jsonObject = basePageRequest.getJsonObject().fluentPut("type", 11);
        basePageRequest.setJsonObject(jsonObject);
        renderJson(adminSceneService.filterConditionAndGetPageList(basePageRequest));
    }
    
    /**
     * @author lg
     * 根据id删除售前申请
     */
    @Permissions("crm:preSales:delete")
    @NotNullValidate(value = "preSalesIds", message = "售前id不能为空")
    public void deleteByIds(@Para("preSalesIds") String preSalesIds) {
        renderJson(crmPreSalesService.deleteByIds(preSalesIds));
    }
}
